#include "smarthome.h"
#include "ui_smarthome.h"

smarthome::smarthome(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::smarthome)
{
    ui->setupUi(this);
    slotOpenSerialPort();
//    ui->progressBar_1->setRange(0,100);//温度 temperature
//    ui->progressBar_1->setValue(78);
//    ui->progressBar_2->setRange(0,100);//湿度 humidity
//    ui->progressBar_2->setValue(50);

    yinyue=new music();
    tianqi =new weather();

    //启动QTimer，每1000毫秒（1秒）触发一次
    startTimer(1000);
    //timer->start(1000);
    // 立即更新时间
    updateTime();
}

smarthome::~smarthome()
{
    delete ui;
}

void smarthome::updateTime()
{
    // 获取当前时间
    QTime currentTime = QTime::currentTime();
    // 将时间格式化为字符串
    QString timeString = currentTime.toString("hh:mm:ss");
    // 设置QLabel的文本
    ui->label_time->setText(timeString);
}

void smarthome::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(0,0,width(),height(),QPixmap("d:/image/home1.jpg"));
}

void smarthome::slotLoadSerialPort()
{
    QStringList m_serialPortName;
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) //获取有效的串口列表
    {
        m_serialPortName << info.portName();
        qWarning()<<"serialPortName:"<<info.portName();
    }
}
//打开串口
void smarthome::slotOpenSerialPort()
{
    m_serialPort = new QSerialPort(this);//新建一个窗口对象

    if(m_serialPort->isOpen())//判断对象是否已经打开，如果是就关掉
    {
        m_serialPort->clear();
        m_serialPort->close();
    }
    //设置串口名字
    m_serialPort->setPortName("COM11");
    //打开串口
    if (!m_serialPort->open(QIODevice::ReadWrite))//打开COM2串口，并且设置为读写状态
    {
        return;
    }

    m_serialPort->setBaudRate(9600);//波特率
    m_serialPort->setDataBits(QSerialPort::Data8);//数据位
    m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//数据流控制（暂时不管）

    m_serialPort->setParity(QSerialPort::NoParity);//奇偶校验位
    m_serialPort->setStopBits(QSerialPort::OneStop);//停止位


    //把串口的可读信号和槽函数连接
    connect(m_serialPort,SIGNAL(readyRead()),this,SLOT(slotReadData()));

}

void smarthome::timerEvent(QTimerEvent *event)
{
    updateTime();
}

void smarthome::slotReadData()
{
    QString data = m_serialPort->readAll();
    qWarning()<<"接收数据："<<data;

    QString str1=data.left(5);
    //QString str2=data.right(6);
    if(str1=="室内温度:")
    {
        ui->label_wendu->setText(data.mid(5,4));
        ui->label_shidu->setText(data.right(3));
    }

}
//关闭串口
void smarthome::slotCloseSerialPort()
{
    if (m_serialPort)
        m_serialPort->close();
}
//发送信息函数
void smarthome::slotSend(QString info)
{
    if(m_serialPort){
        if(!m_serialPort->isOpen()){
            qWarning()<<"请打开一个串口";
         }
       // QString info ="hello";
        QByteArray sendBuf;
        //把字符串转成字节
        sendBuf.append(info);
        m_serialPort->write(sendBuf);

    }
}

void smarthome::on_pushButton_clicked()
{

    QString info;
    if(isopen==true){
         info ="打开风扇";

    }else{
         info ="关闭风扇";
    }
    slotSend(info);
    isopen=!isopen;
    qDebug()<<info<<endl;

}
void smarthome::on_pushButton_kongtiao_clicked()
{
    QString info;
    if(kt==true){
         info ="打开空调";

    }else{
         info ="关闭空调";
    }
    slotSend(info);
    kt=!kt;
    qDebug()<<info<<endl;
}

void smarthome::on_pushButton_light_clicked()
{
    QString info;
    if(deng==true){
         info ="打开灯";

    }else{
         info ="关闭灯";
    }
    slotSend(info);
    deng=!deng;
    qDebug()<<info<<endl;
}

void smarthome::on_pushButton_weather_clicked()
{
//    close();
    tianqi->show();
}

void smarthome::on_pushButton_music_clicked()
{
//    close();
    yinyue->show();

}








